<?php
/**
 * Created by PhpStorm.
 * User: sosyuki
 * Date: 2018/6/11
 * Time: 9:25
 */

namespace Drupal\gateway_client;

use GatewayClient\Gateway;

class GatewayClient {

  protected $client;

  static function init() {
    Gateway::$registerAddress = variable_get_value('gateway_client_ip');
  }

  public static function bindUid($client_id, $uid) {
    $prefix = self::getPrefix();
    Gateway::bindUid($client_id, $prefix . $uid);
    Gateway::joinGroup($client_id, $prefix);
  }

  static function getPrefix() {
    return $_SERVER['SERVER_NAME'];
  }

  function joinGroup($client_id, $group) {
    $prefix = self::getPrefix();
    Gateway::joinGroup($client_id, $prefix . $group);
  }

  function joinGroupUid($uid, $group) {
    $prefix = self::getPrefix();
    $client_ids = Gateway::getClientIdByUid($prefix . $uid);
    foreach ($client_ids as $client_id) {
      Gateway::joinGroup($client_id, $prefix . $group);
    }
  }

  function pushUid($uid, $key, array $data) {
    $message = [
      'type' => $key,
      'data' => $data,
    ];
    $prefix = self::getPrefix();
    Gateway::sendToUid($prefix . $uid, drupal_json_encode($message));
    return TRUE;
  }

  function pushUids(array $uids, $key, array $data) {
    $prefix = self::getPrefix();
    $sendUids = [];
    foreach ($uids as $uid) {
      $sendUids[] = $prefix . $uid;
    }
    $message = [
      'type' => $key,
      'data' => $data,
    ];
    Gateway::sendToUid($sendUids, drupal_json_encode($message));
    return TRUE;
  }

  function pushAll($key, array $data) {
    $message = [
      'type' => $key,
      'data' => $data,
    ];
    $prefix = self::getPrefix();
    Gateway::sendToGroup($prefix, drupal_json_encode($message));
    return TRUE;
  }

  function pushGroup($group, $key, array $data) {
    $message = [
      'type' => $key,
      'data' => $data,
    ];
    $prefix = self::getPrefix();
    Gateway::sendToGroup($prefix . $group, drupal_json_encode($message));
    return TRUE;
  }

  function pushTimer(int $time, string $key, string $callback) {
    $prefix = self::getPrefix();
    $sendData = [
      'type' => 'event',
      'key' => $prefix . $key,
      'time' => $time,
      'host' => $_SERVER['SERVER_NAME'],
      'port' => $_SERVER['SERVER_PORT'],
      'uri' => $callback,
    ];
    $client = $this->getClient();
    // 发送数据，注意5678端口是Text协议的端口，Text协议需要在数据末尾加上换行符
    fwrite($client, drupal_json_encode($sendData) . "\n");
    // 读取推送结果
    return fread($client, 8192);
  }

  protected function getClient() {
    if ($this->client) {
      return $this->client;
    }
    // 建立socket连接到内部推送端口
    $ip = variable_get_value('gateway_client_text_ip');
    $this->client = stream_socket_client("tcp://{$ip}", $errno, $errmsg, 1);
    return $this->client;
  }

  function rmTimer(string $key) {
    $prefix = self::getPrefix();
    $sendData = [
      'type' => 'rm_event',
      'key' => $prefix . $key,
    ];
    $client = $this->getClient();
    // 发送数据，注意5678端口是Text协议的端口，Text协议需要在数据末尾加上换行符
    fwrite($client, drupal_json_encode($sendData) . "\n");
    // 读取推送结果
    return fread($client, 8192);
  }
}
